<?php
/***************************************************************
*  Copyright notice
*  (c) 2009, jianyuzhu@gmail.com
*  All rights reserved
*  This script is part of the PPEMI project.
***************************************************************/

class UserAddressBase extends UserBase {
	//type
	//	address,shipping,payment,billing
	var $type = '';
	
	//constructor
	function UserAddressBase() {
		parent::UserBase();
	}
	
	//methods
	function getItems($filter = '', $check = 0, $limit = true) {
		$query_limit_str = "";
		if( $limit == true ) {
			$query_limit_str = $this->getQueryLimitStr();
		}
		
		$query_str = "";
		if( $check == -1 ) {
			//
		} else {
			//$query_str .= " and address_status = 1";
		}
		$query_str .= $this->_query_type();
		if( is_array($filter) ) {
			$filter = func_db_input($filter);
			if( isset($filter['type']) ) {
				$query_str .= " and ua.address_type = '" . $filter['type'] . "' ";
			}
			if( isset($filter['customer']) ) {
				$query_str .= " and u.users_type = 'customer' ";
			}
		}
		
		//$query = "select * from pp_users_address where " . $this->_query_owner('users_id') . " " 
		//		. $query_str . " order by address_address asc, address_id asc";
		$query = "select ua.*, z.*, c.*, " 
				. " if(ua.address_zones_id > 0, z.zones_name, ua.address_state) as statename, " 
				. " if(ua.address_zones_id > 0, z.zones_code, ua.address_state) as statecode, " 
				. " if(ua.address_countries_id > 0, c.countries_name, ua.address_country) as countryname, " 
				. " if(ua.address_countries_id > 0, c.countries_iso_code_3, ua.address_country) as countrycode " 
				. " from pp_users_address ua left join pp_zones z on ua.address_zones_id = z.zones_id " 
				. " left join pp_countries c on ua.address_countries_id = c.countries_id where 1 and " . $this->_query_owner('ua.users_id') . " "
				. $query_str . " "
				. " order by ua.address_id asc "
				. $query_limit_str;
		
		return $this->adb->getRows($query);
	}
	
	function getItemsTotal($filter = '', $check = 0) {
		$query_str = "";
		if( $check == -1 ) {
			//
		} else {
			//$query_str .= " and address_status = 1";
		}
		$query_str .= $this->_query_type();
		if( is_array($filter) ) {
			$filter = func_db_input($filter);
			if( isset($filter['type']) ) {
				$query_str .= " and ua.address_type = '" . $filter['type'] . "' ";
			}
			if( isset($filter['customer']) ) {
				$query_str .= " and u.users_type = 'customer' ";
			}
		}
		
		return $this->adb->getCountA('pp_users_address', "" . $this->_query_owner('users_id') . " " . $query_str . " ");
	}
	
	//
	function checkItem($items_id) {
		$query = "select address_id from pp_users_address where " . $this->_query_owner('users_id') . " and address_id = '" . (int)$items_id . "'";
		
		return $this->adb->checkCount($query);
	}
	
	function getItem($items_id) {
		$query = "select ua.*, z.*, c.*, " 
				. " if(ua.address_zones_id > 0, z.zones_name, ua.address_state) as statename, " 
				. " if(ua.address_zones_id > 0, z.zones_code, ua.address_state) as statecode, " 
				. " if(ua.address_countries_id > 0, c.countries_name, ua.address_country) as countryname, " 
				. " if(ua.address_countries_id > 0, c.countries_iso_code_3, ua.address_country) as countrycode " 
				. " from pp_users_address ua left join pp_zones z on ua.address_zones_id = z.zones_id " 
				. " left join pp_countries c on ua.address_countries_id = c.countries_id " 
				. " where " . $this->_query_owner('ua.users_id') . " and address_id = '" . (int)$items_id . "'";
		
		return $this->adb->getRow($query);
	}
	
	function addItem($data_array) {
		if( !$this->_not_null($data_array) || !is_array($data_array) ) {
			return false;
		}
		
		$data_array = func_db_input($data_array);
		$data_array['users_id'] = $this->owner;
		$data_array['address_type'] = $this->_get_type();
		
		$items_id = $this->adb->insertA('pp_users_address', $data_array);
		
		return $items_id;
	}
	
	function addItems($data_array) {
		$items_ids = array();
		if( is_array($data_array) ) {
			foreach($data_array as $k => $v) {
				$items_ids[] = $this->addItem($v);
			}
		}
		
		return $items_ids;
	}
	
	function updateItem($items_id, $data_array) {
		if( !$this->_not_null($items_id) || !$this->_not_null($data_array) || !is_array($data_array) ) {
			return false;
		}
		
		$data_array = func_db_input($data_array);
		$data_array['address_last_modified'] = 'now()';
		$data_array['address_modified_by'] = $this->_get_current_user();
		
		return $this->adb->updateA('pp_users_address', $data_array, "" . $this->_query_owner('users_id') . " and address_id = '" . (int)$items_id . "'");
	}
	
	function deleteItem($items_id) {
		if( !$this->_not_null($items_id) ) {
			return false;
		}
		
		$this->adb->deleteA('pp_users_address', "" . $this->_query_owner('users_id') . " and address_id = '" . (int)$items_id . "'");
		
		return true;
	}
	
	function deleteItems($items_ids) {
		if( is_array($items_ids) ) {
			$this->_delete_ins('pp_users_address', 'address_id', $items_ids, $this->_query_owner('users_id'));
		}
	}
	
	function setItemsStatus($items_ids, $status = 1) {
		if( is_array($items_ids) ) {
			$this->_update_ins('pp_users_address', "address_status = '" . (int)$status . "'", 'address_id', $items_ids);
		}
	}
	
	//
	function check($items_id) {
		$count = $this->adb->getCountA('pp_users_address', "address_id = '" . (int)$items_id . "'");
		
		return ($count > 0) ? true : false;
	}
	
	function get($categories_id) {
		return $this->adb->getRow('pp_pages_categories', "categories_id = '" . (int)$categories_id . "'");
	}
	
	function add($data_array) {
		if( !$this->_not_null($data_array) || !is_array($data_array) ) {
			return false;
		}
		
		$data_array = func_db_input($data_array);
		
		return $this->adb->insertA('pp_users_address', $data_array);
	}
	
	function update($items_id, $data_array) {
		if( !$this->_not_null($items_id) ) {
			return false;
		}
		
		$data_array = func_db_input($data_array);
		$data_array['address_last_modified'] = 'now()';
		$data_array['address_modified_by'] = $this->_get_current_user();
		
		return $this->adb->updateA('pp_users_address', $data_array, "address_id = '" . (int)$items_id . "'");
	}
	
	function delete($items_id) {
		return $this->adb->deleteA('pp_users_address', "address_id = '" . (int)$items_id . "'");
	}
	
	//private
	
	//type
	function _query_type($p = '', $type = '') {
		$query = "";
		$types = array('address', 'shipping', 'billing', 'payment');
		$type = ($type == '') ? $this->type : '';
		if( substr($type, 0, 1) == '!' ) {
			$type = substr($type, 1);
			$query .= " and (";
			$i = 0;
			foreach($types as $k => $v) {
				if( $v != $type ) {
					if( $i > 0 ) {
						$query .= " or ";
					}
					$query .= (($p == '') ? '' : $p . ".") . "address_type = '" . $v . "' ";
					$i++;
				}
			}
			$query .= ") ";
			
			return $query;
		} elseif( strstr($type, '|') !== false ) {
			$query .= " and (";
			$i = 0;
			$ts = explode("|", $type);
			foreach($ts as $k => $v) {
				if( $i > 0 ) {
					$query .= " or ";
				}
				$query .= (($p == '') ? '' : $p . ".") . "address_type = '" . $v . "' ";
				$i++;
			}
			$query .= ") ";
			
			return $query;
		} else {
			if( in_array($type, $types) ) {
				return " and " . (($p == '') ? '' : $p . ".") . "address_type = '" . $type . "' ";
			}
		}
		
		return '';
	}
	
	function _get_type() {
		$types = array('address', 'shipping', 'billing', 'payment');
		if( in_array($this->type, $types) ) {
			return $this->type;
		}
		
		return '';
	}
}

class UserAddressCustomerBase extends UserAddressBase {
	
	//constructor
	function UserAddressCustomerBase() {
		$this->type = 'address';
		
		parent::UserAddressBase();
	}
}

class UserAddressShippingBase extends UserAddressBase {
	
	//constructor
	function UserAddressShippingBase() {
		$this->type = 'shipping';
		
		parent::UserAddressBase();
	}
}

class UserAddressPaymentBase extends UserAddressBase {
	
	//constructor
	function UserAddressPaymentBase() {
		$this->type = 'payment';
		
		parent::UserAddressBase();
	}
}

class UserAddressBillingBase extends UserAddressBase {
	
	//constructor
	function UserAddressBillingBase() {
		$this->type = 'billing';
		
		parent::UserAddressBase();
	}
}
//
?>